package com.volcreate.exception_collection.no7;

import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 * @author zxc
 * @date 2020/10/6
 */
public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        try {
            while (true) {
                String s = scanner.nextLine();
                if ("other".equals(s)) {
                    break;
                }
                if ("new".equals(s)) {
                    boolean f = true;
                    String info = scanner.nextLine();
                    String[] split = info.split("\\s");

                    Student student = new Student();
                    try {
                        student.setName(split[0]);
                    } catch (Exception e) {
                        f = false;
                        System.out.println(e);
                    }

                    try {
                        if (split.length < 2) {
                            throw new NoSuchElementException();
                        }
                        student.addScore(Integer.parseInt(split[1]));
                    } catch (Exception e) {
                        f = false;
                        System.out.println(e);
                    }

                    if (f) {
                        System.out.println(student);
                    }
                }
            }
        } finally {
            System.out.println("scanner closed");
        }


    }
}

class Student {
    private String name;
    private int score;

    public String getName() {
        return name;
    }

    public void setName(String name) throws IllegalNameException {
        if (name.matches("\\d\\w*")) {
            throw new IllegalNameException("the first char of name must not be digit, name=" + name);
        }
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student [" +
                "name=" + name +
                ", score=" + score +
                ']';
    }


    public int addScore(int score) throws IllegalScoreException {
        int newScore = this.score + score;
        if (newScore > 100 || newScore < 0) {
            throw new IllegalScoreException("score out of range, score=" + newScore);
        }
        this.score = newScore;
        return newScore;
    }  //如果加分后分数<0 或>100，则抛出IllegalScoreException，加分不成功。

}

class IllegalNameException extends Exception {

    public IllegalNameException(String message) {
        super(message);
    }
}

class IllegalScoreException extends Exception {
    public IllegalScoreException(String message) {
        super(message);
    }
}